<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}


/**
 * Description of sms
 * 阿福信用记录接口
 * @author hsisme
 */
class credit {

    public static $errInfo = null;
    public static $config = array();
    public static $uid = 'renrenyong';
    public static $rc4Key = '8e3f87c9381eef8e';
    public static $url = "http://www.zhichengcredit.com/CreditPortal/api/queryLoan/V2?";
    public static $privateKeyPath = '';

    static public function init() {
        $creditConfig = C('app/pay');
        self::$privateKeyPath = $creditConfig['alipay']['private_key'];
    }

    static public function send($objData) {
        self::init();
        self::$config['idNo']        = $objData['card_num'];
        self::$config['idType']      = '101';
        self::$config['name']        = $objData['username'];
        self::$config['queryReason'] = '10';
        $uidStr    = self::rsaPublicSign(self::$uid , self::$privateKeyPath);
        $paramStr  = self::rc4Encode(self::$rc4Key , json_encode(self::$config));
        $paramStr  = base64_encode($paramStr);
        $params['userid'] = $uidStr;
        $params['params'] = $paramStr;
        $result = getHttp(self::$url , $params);
        $result = json_decode($result , true);
        if($result['errorcode'] == '0000'){
            $objData = base64_decode($result['data']);
            $userInfo = json_decode(urldecode(self::rc4Encode(self::$rc4Key,$objData)), true);
            return $userInfo;
        }else{
            return false;
        }
    }


    static public function gerError() {
        return self::$errInfo;
    }


    //RSA加密
    static public function rsaPublicSign($data, $private_key_path) {
        $priKey = file_get_contents($private_key_path);
        $res = openssl_pkey_get_public($priKey);
        
        openssl_public_encrypt($data, $sign, $res);
        openssl_free_key($res);
        //base64编码
        $sign = base64_encode($sign);

        return $sign;
    }


     /**
     * 加密
     * @param string $key 私匙
     * @param mix $data 需要加密的数据
     * @param boolean $decrypted 是否解密
     * @return 16进制字符串
     */
    static public function rc4Encode($pwd, $data) {
        $key [] = "";
        $box [] = "";
        $pwd_length = strlen ( $pwd );
        $data_length = strlen ( $data );
        $cipher = "";

        for($i = 0; $i < 256; $i ++) {
            $key [$i] = ord ( $pwd [$i % $pwd_length] );
            $box [$i] = $i;
        }
        
        for($j = $i = 0; $i < 256; $i ++) {
            $j = ($j + $box [$i] + $key [$i]) % 256;
            $tmp = $box [$i];
            $box [$i] = $box [$j];
            $box [$j] = $tmp;
        }
        
        for($a = $j = $i = 0; $i < $data_length; $i ++) {
            $a = ($a + 1) % 256;
            $j = ($j + $box [$a]) % 256;
            
            $tmp = $box [$a];
            $box [$a] = $box [$j];
            $box [$j] = $tmp;
            
            $k = $box [(($box [$a] + $box [$j]) % 256)];
            $cipher .= chr ( ord ( $data [$i] ) ^ $k );
        }
        return $cipher;
    }

}
